package com.partybuilding.Controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.partybuilding.annotation.DataScope;
import com.partybuilding.annotation.PrivacyEncryptMethod;
import com.partybuilding.annotation.SystemLog;
import com.partybuilding.domain.ResponseResult;
import com.partybuilding.domain.entity.Admintable;
import com.partybuilding.domain.entity.LoginUser;
import com.partybuilding.domain.entity.Usertable;
import com.partybuilding.domain.vo.UserCheckVo;
import com.partybuilding.enums.AppHttpCodeEnum;
import com.partybuilding.enums.DesensitizationEnum;
import com.partybuilding.listener.EasyExcelOrderListener;
import com.partybuilding.service.UsertableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wulala
 */
@RestController
@RequestMapping("/usertable")
@Api(tags = "党员",description="党员交互相关接口")
public class UsertableController {

    @Autowired
    private UsertableService usertableService;

    @GetMapping("/list")
    public List<Usertable> getList() {
        return usertableService.list();
    }
    @PostMapping("/test")
    @SystemLog(busnissName = "测试接口")
    public ResponseResult upDateOneUser(@RequestBody Usertable usertable) {
        ResponseResult result=usertableService.upDateOneUser(usertable);
        return result;
    }

    @PostMapping("setOrGetKey")
    public ResponseResult setOrGetKey(){
        ResponseResult result=usertableService.setOrGetKey();
        return result;
    }

    @PostMapping("/writePartyTime")
    public ResponseResult  writePartyTime(){
        ResponseResult result= usertableService.writePartyTime();
        return result;
    }

    @GetMapping("/getAllUserDesensitize")
    @PreAuthorize("hasAuthority('getAllData')")//用于权限控制
    @PrivacyEncryptMethod(desensitize = DesensitizationEnum.DESENSITIZE)
    public ResponseResult getAllUserDesensitize(){
        LambdaQueryWrapper<Usertable> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        ResponseResult result=usertableService.getAllUser(lambdaQueryWrapper);
        return result;
    }

    @GetMapping("/getAllUser")
    @PreAuthorize("hasAuthority('getAllData')")//用于权限控制
    @PrivacyEncryptMethod(desensitize = DesensitizationEnum.NO_DESENSITIZE)//用于脱敏
    public ResponseResult getAllUser(){
        LambdaQueryWrapper<Usertable> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        ResponseResult result=usertableService.getAllUser(lambdaQueryWrapper);
        return result;
    }

    @GetMapping("/getUser/{id}")
    @PreAuthorize("hasAuthority('getAllData')")//用于权限控制

    @PrivacyEncryptMethod(desensitize = DesensitizationEnum.NO_DESENSITIZE)//用于脱敏
    public ResponseResult getUser(@PathVariable("id") Long id){

        LambdaQueryWrapper<Usertable> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        ResponseResult result=usertableService.getUser(id,lambdaQueryWrapper);
        return result;
    }

    @PostMapping("/uploadAllData")
    @ApiOperation(value = "党员信息上传",notes = "上传党员信息excel表格，并在数据库中完成插入或更新")//用于接口文档生成
    public ResponseResult uploadAllData() {
        String fileUrl = "D:\\important document\\code\\partyBuilding\\partyBuilding\\src\\main\\resources\\static\\test.xlsx";
        ResponseResult result;
        try {
            EasyExcel.read(fileUrl, UserCheckVo.class, new EasyExcelOrderListener(usertableService)).sheet().doRead();
            result = ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            result = ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg());
            throw new RuntimeException("表格上传失败"+e);
        }
        return result;
    }


}
